package com.genghis.ptas.comment;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.comment.entity.Comment;
import com.genghis.ptas.comment.service.CommentService;
import com.genghis.ptas.scorm.service.UpScormService;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @author tiangc
 * @version 1.0.1
 */
@Controller
@Transactional
@RequestMapping("/comment/")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    @Autowired
    private UpScormService upScormService;

    @RequestMapping(value = "saveComment", method = {RequestMethod.POST})
    @ResponseBody
    public void saveComment(Comment comment) {
        int userId = userService.findUserByLoginName(LoginUserUtil.findLoginUserName()).getId();
        commentService.addScormComment(comment,userId);
    }


    @RequestMapping(value = "viewCommentDo", method = {RequestMethod.GET})
    public String viewCommentDo(HttpServletRequest request,@RequestParam("id") int id) {
        request.setAttribute("scormId",id);
        return "comment/listCommentInfoDo";
    }


    @RequestMapping(value = "listScormComment", method = {RequestMethod.POST})
    @ResponseBody
    public Page<Comment> listScormComment(PageParameter pageParameter, Comment comment) {
       Page<Comment> commentPage= commentService.listScormComment(pageParameter,comment);
        return commentPage;
    }

    @RequestMapping(value = "delComment", method = {RequestMethod.DELETE})
    @ResponseBody
    public void delComment(@RequestParam("ids") String ids)  {
        String[] delIds = ids.split(",");
        commentService.deleteCommentById(delIds);
    }

    @RequestMapping(value = "editCommentDo", method = {RequestMethod.GET})
    public String editCommentDo(HttpServletRequest request,@RequestParam("id") int id) {
        Comment comment=commentService.findCommentById(id);
        request.setAttribute("commentInfo",comment);
        return "comment/commentInfo";
    }

    @RequestMapping(value = "editComment", method = {RequestMethod.POST})
    @ResponseBody
    public void editComment(Comment comment)  {

        commentService.editComment(comment);
    }

}
